/**
 * @Author: 陈瑞鹏
 * @Date:   2022-09-23 11:09:17
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-12-30 09:31:51
 */

import { Dictionary } from "./Dictionary";


export class SoundResManager {
  
  private static _instance: SoundResManager = null;

  //音效名字与音效路径的映射
  private _dictResList: Dictionary<string, cc.AudioClip> = null;

  private constructor() {
  }

  public static getInstance(): SoundResManager {
    if (!SoundResManager._instance) {
      SoundResManager._instance = new SoundResManager();
      SoundResManager._instance._dictResList = new Dictionary<string, cc.AudioClip>();
    }

    return SoundResManager._instance;
  }

  public preload(filePath: string): void {
    // ++this._nResTotalCount;
    // PreLoadCountManager.getInstance().setResTotalCount(PreLoadCountManager.getInstance().getResTotalCount() + 1);

    // let loader:any = cc.loader;
    let that: SoundResManager = this;

    cc.resources.loadDir(filePath, cc.AudioClip, function () { }, function (err, asset) {
      // ++that._nResCurrLoadedCount;
      // PreLoadCountManager.getInstance().addResCurrLoadedCount();
      // //cc.log("### DynamicResManager 预加载完成:",filePath);

      // 检查资源加载
      if (err) {
        // console.warn( '###预加载:',filePath,"失败， 原因:", JSON.stringify(err) );
        console.warn('###预加载:', filePath, "失败， 原因:", JSON.stringify(asset));
        return;
      }

      // console.warn( '###预加载:',filePath,"成功， 原因:", JSON.stringify(asset) );
      for (let k = 0; k < asset.length; ++k) {
        let clip: any = asset[k];
        let name: string = clip.name;
        that._dictResList.set(name, clip);
      }

    });
  }

  public getResPathWithFileName(fileName: string): cc.AudioClip {
    let clip: cc.AudioClip = this._dictResList.get(fileName);
    return clip;
  }

  private _bgmAudioid: number = -1;
  public PlayBGM() {

    if (this._bgmAudioid != -1 && this._bgmAudioid != null) {
      cc.audioEngine.stopMusic();
    }

    let path: cc.AudioClip = this.getResPathWithFileName('qqsanguoBgm');
    if (!path) {
      cc.warn("播放音效:", 'qqsanguoBgm', ' 不存在');
      return -1;
    }

    this._bgmAudioid = cc.audioEngine.playMusic(path, true);

    cc.audioEngine.setVolume(this._bgmAudioid, 0.3);

  }

  public Play(fileName: string, loop: boolean = false, volume: number = 1): number {
    // if(!GameCommonData.getInstance().isSoundOpen)
    // {
    //     return -1;
    // }

    let path: cc.AudioClip = this.getResPathWithFileName(fileName);

    if (!path) {
      cc.warn("播放音效:", fileName, ' 不存在');
      // TipsTool.tipsCenterBottomUp("播放音效:"+fileName+' 不存在');
      return -1;
    }

    return cc.audioEngine.playEffect(path, loop);
    // return cc.audioEngine.play(path,loop,volume);
  }

  public Stop() {
    cc.audioEngine.stopAllEffects();
  }

  public StopBGM() {
    cc.audioEngine.stopMusic();
    this._bgmAudioid = -1;
  }
}
